package approve.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import resource.pojo.Approvedoc;

import model.pojo.Employee;
import nguyen.dao.ListDocuments;
import nguyen.dao.MyDocumentDAO;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class My_DailyReport14 extends Activity{
	EditText title, description, content;
	TextView time;
	Button submitBtn;
	String lang = "EN",database="admin";//Delete
	
	Bundle language;
	Approvedoc appDoc;
	ArrayList<String> arr = new ArrayList<String>();
	ListDocuments call = new ListDocuments();
	ArrayList<Employee> list = new ArrayList<Employee>();
	
	Button re_delete;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_dailyreport14);
		loadBunlde();
		loadLanguge();
		
		re_delete=(Button) findViewById(R.id.re_delete);
		
		initFormLoad();
	}
	public void loadLanguge(){
		TextView la_title=(TextView) findViewById(R.id.la_title);
		TextView la_description=(TextView) findViewById(R.id.la_description);
		TextView la_content=(TextView) findViewById(R.id.la_content);
		
		la_title.setText(language.getString("title"));
		la_description.setText(language.getString("Description"));
		la_content.setText(language.getString("content"));
	}
	
	public void loadBunlde(){
		Bundle extras = getIntent().getExtras();
		lang = extras.getString("lang");
		database = extras.getString("database");
		language = extras.getBundle(lang);
		appDoc=(Approvedoc) extras.get("appDoc");
	}
	
	public void initFormLoad(){
		Calendar c = Calendar.getInstance();
		String timeText = c.get(Calendar.YEAR) +" - "+ (c.get(Calendar.MONTH) +1) +" - "+ c.get(Calendar.DAY_OF_MONTH); 
		time = (TextView)findViewById(R.id.dailyreport_time);
		time.setText(timeText);
		title = (EditText) findViewById(R.id.dailyreport_title);
		description = (EditText) findViewById(R.id.dailyreport_description);
		content = (EditText) findViewById(R.id.dailyreport_content);
		
		//load data by appDoc
		
		title.setText(appDoc.getTitle());
		description.setText(appDoc.getDescription());
		content.setText(Html.fromHtml(appDoc.getContent()));
		
		submitBtn = (Button) findViewById(R.id.dailyreport_sending);
		submitBtn.setText(language.getString("document_send"));
		submitBtn.setOnClickListener(submitSend);
		
		re_delete.setText(language.getString("Delete"));
		re_delete.setOnClickListener(deleteDoc);
		
		
	}
	
	private void warningDelete() {
		AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
		 helpBuilder.setTitle(language.getString("Delete"));
		 final TextView textView = new TextView(this);
		 textView.setText(language.getString("are-you-sure"));
		 helpBuilder.setView(textView);
		 helpBuilder.setPositiveButton(language.getString("ok"), new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int which) {
		    	MyDocumentDAO.deleteDoc(database, appDoc.getiDApproveDoc());
		    	
				Intent myIntent = new Intent(My_DailyReport14.this,
						My_App_SavedDocument.class);
				myIntent.putExtras(getIntent().getExtras());
				startActivity(myIntent);
				finish();
		    }
		   });

		 helpBuilder.setNegativeButton(language.getString("cancel"), new DialogInterface.OnClickListener() {
		
		  public void onClick(DialogInterface dialog, int which) {
		   // Do nothing
		  }
		 });
		 // Remember, create doesn't show the dialog
		 AlertDialog helpDialog = helpBuilder.create();
		 helpDialog.show();

	}
	
//	public Approvedoc getApproveDoc(){
//		//return MyDocumentDAO.getDocumentDetailForm12(database, iDApproveDoc);
//	}

	
	private OnClickListener deleteDoc=new OnClickListener() {
		
	
		public void onClick(View arg0) {
			
			warningDelete();
			
		}
	};
	
	private OnClickListener submitSend = new OnClickListener() {
		

		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			String titleApp = title.getText().toString();
			String descriptionApp = description.getText().toString();
			Date date = new Date();
			String IDGroup = "" + date.getTime();
//			Employee emp = (Employee) getIntent().getExtras().get("em");
//			appDoc = new Approvedoc();
			appDoc.setTitle(titleApp);
			appDoc.setiDGroupApprove(IDGroup);
			appDoc.setDescription(descriptionApp);
			appDoc.setContent(Html.toHtml(content.getText()));
//			appDoc.setiDEmployee(emp.getIdemployee());
			getAllEmpsName();
			showDialog(0);
		}
	};
	String[] _options;
	protected boolean[] _selections;
	Integer count = 0;
	ArrayList<String> emps = new ArrayList<String>();

	@Override
	protected Dialog onCreateDialog(int id) {
		return new AlertDialog.Builder(this)
				.setTitle(R.string.approver)
				.setMultiChoiceItems(loadOptions(), loadSelections(),
						new DialogSelectionClickHandler())
				.setPositiveButton(language.getString("ok"), new DialogButtonClickHandler())
				.setNegativeButton(language.getString("cancel"),new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						
					}
				})
				.create();
	}

	public String[] loadOptions() {
		_options = new String[arr.size()];
		for (int i = 0; i < arr.size(); i++) {
			_options[i] = arr.get(i);
		}
		return _options;
	}

	public boolean[] loadSelections() {
		_selections = new boolean[_options.length];
		return _selections;
	}

	public class DialogSelectionClickHandler implements
			DialogInterface.OnMultiChoiceClickListener {
		public void onClick(DialogInterface dialog, int clicked,
				boolean selected) {
			if (selected == true) {
				emps.add(_options[clicked].toString());
				count++;
			} else {
				for (int i = 0; i < emps.size(); i++) {
					if (_options.toString().equals(emps.get(i))) {
						emps.remove(i);
						count--;
					}
				}
			}
		}
	}

	public class DialogButtonClickHandler implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int clicked) {
			switch (clicked) {
			case DialogInterface.BUTTON_POSITIVE:
				sendDocument();
				
				Intent myIntent = new Intent(My_DailyReport14.this,
						My_App_SavedDocument.class);
				myIntent.putExtras(getIntent().getExtras());
				
				startActivity(myIntent);
				
			}
		}
	}

	public  void sendDocument() {
		System.out.println("database:"+database);
		String[] selects = new String[count];
		int j = 0;
		for (int i = 0; i < emps.size(); i++) {
			for (Employee emp : list) {
				if (emp.getFullName().equals(emps.get(i).toString())) {
					emps.set(i, emp.getIdemployee());
					selects[j] = emps.get(i).toString();
					j++;
				}
			}
		}
		MyDocumentDAO.sendDocument(database, appDoc,selects);
		
		
		
	}
	
	

	public ArrayList<Employee> getList() {
		return list;
	}

	public void setList(ArrayList<Employee> list) {
		this.list = list;
	}

	public  void getAllEmpsName() {
		list = call.getAllEmp(database);
		for (int i = 0; i < list.size(); i++) {
			arr.add(list.get(i).getFullName());
		}
	}
}
